<?php

require_once('../simpletest/autorun.php');
require_once('../../system/core/Document.php');
require_once('../../system/components/DocumentComponent.php');
require_once('../../system/components/DocumentValueComponent.php');
require_once('../../system/exceptions/NullComponentException.php');



/**
 * TestOfDocument
 *
 * @author Pavel Egorov 2011
 */
class TestComponent extends system\components\DocumentComponent {

    public $testVariable;

    function __construct() {
        parent::__construct('TestComponent');
        $this->testVariable = 'testvalue';
    }

}


class TestOfDocument extends UnitTestCase {

    function testIsDocumentContainsAtLeastRootElement() {
        // Setup
        $document = new system\core\Document();

        // Test
        $xml = $document->getXml();

        // Verify
        $element = $xml->getElementsByTagName('root');
        $this->assertNotNull($element);
        $this->assertNotEqual($element->length, 0);
        $this->assertEqual($element->item(0)->nodeName, 'root');
    }


    function testIfNullComponentAddedExceptionIsThrowed() {
        // Setup
        $document = new system\core\Document();

        // Test
        try {
            $document->add(NULL);
        } catch (Exception $e) {
            $document = NULL;
        }

        // Verify
        $this->assertNull($document);
    }

    function testIfDocumentConainsInsertedDocumentComponent() {
        // Setup
        $document = new system\core\Document();
        $documentComponent = new TestComponent();
        $document->add($documentComponent);

        // Test
        $xml = $document->getXml();

        // Verify
        $element = $xml->getElementsByTagName('TestComponent');
        $this->assertNotNull($element);
        $this->assertNotEqual($element->length, 0);
        $this->assertEqual($element->item(0)->nodeName, 'TestComponent');

        $testComponent = $element->item(0);
        $testVariable = $xml->getElementsByTagName('testVariable');
        $this->assertNotNull($testVariable);
        $this->assertNotEqual($testVariable->length, 0);
        $this->assertEqual($testVariable->item(0)->nodeName, 'testVariable');
        $this->assertEqual($testVariable->item(0)->nodeValue, 'testvalue');
    }

    function testIfDocumentConainsInsertedDocumentValueComponent() {
        // Setup
        $document = new system\core\Document();
        $documentValueComponent = new system\components\DocumentValueComponent('TestName','TestValue');
        $document->add($documentValueComponent);

        // Test
        $xml = $document->getXml();

        // Verify
        $element = $xml->getElementsByTagName('TestName');
        $this->assertNotNull($element);
        $this->assertNotEqual($element->length, 0);
        $this->assertEqual($element->item(0)->nodeName, 'TestName');
        $this->assertEqual($element->item(0)->nodeValue, 'TestValue');
    }

    function testIfDocumentConainsInnerComponents() {
        // Setup
        $document = new system\core\Document();
        $documentComponent = new TestComponent();
        $documentValueComponent = new system\components\DocumentValueComponent('TestName','TestValue');
        $documentComponent->add($documentValueComponent);
        $document->add($documentComponent);

        // Test
        $xml = $document->getXml();

        // Verify
        $element = $xml->getElementsByTagName('TestName');

        $this->assertNotNull($element);
        $this->assertNotEqual($element->length, 0);
        $this->assertEqual($element->item(0)->nodeName, 'TestName');
        $this->assertEqual($element->item(0)->nodeValue, 'TestValue');
    }

}
